<template>
  <div class="app-container">
    <!-- 查询-->
    <div class="query">
      <el-form :inline="true" :model="queryparam" class="demo-form-inline">
        <el-form-item label="交易码">
          <el-input v-model="queryparam.tradecode" placeholder="交易码"></el-input>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="queryparam.tradename" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOpen">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--表格-->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="tradename" label="名称"></el-table-column>
      <el-table-column prop="tradecode" label="交易码"></el-table-column>
      <el-table-column prop="flag" label="状态" :formatter="flagType"></el-table-column>
      <el-table-column prop="tradedesc" label="描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="primary" @click="openMod(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="pagesize"
      :page-sizes="[5,10,50,100]"
      layout="total, sizes,prev, pager, next"
      :total="total"
    ></el-pagination>

    <el-dialog title="添加交易" :visible.sync="addVisible">
      <el-form :model="addparam" :rules="addroles" ref="addfrom">
        <el-form-item label="名    称" :label-width="formLabelWidth" prop="tradename">
          <el-input v-model="addparam.tradename" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="交 易 码" :label-width="formLabelWidth" prop="tradecode">
          <el-input v-model="addparam.tradecode" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状    态" :label-width="formLabelWidth" prop="flag">
          <el-radio-group v-model="addparam.flag">
            <el-radio label="0">有效</el-radio>
            <el-radio label="1">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描    述" :label-width="formLabelWidth" prop="tradedesc">
          <el-input v-model="addparam.tradedesc" type="textarea"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="closeAdd">取 消</el-button>
        <el-button type="primary" :disabled="isDisable" @click="addsub">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改交易" :visible.sync="modVisible">
      <el-form :model="modparam" :rules="addroles" ref="modfrom">
        <el-form-item label="名    称" :label-width="formLabelWidth" prop="tradename">
          <el-input v-model="modparam.tradename" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="交 易 码" :label-width="formLabelWidth" prop="tradecode">
          <el-input v-model="modparam.tradecode" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状    态" :label-width="formLabelWidth" prop="flag">
          <el-radio-group v-model="modparam.flag">
            <el-radio label="0">有效</el-radio>
            <el-radio label="1">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描    述" :label-width="formLabelWidth" prop="tradedesc">
          <el-input v-model="modparam.tradedesc" type="textarea"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="closeMod">取 消</el-button>
        <el-button type="primary" :disabled="isDisable" @click="subMod">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import basemixni from "@/mixins/basemixin";
import { mod, add, del, list } from "@/api/trade";

export default {
  mixins: [basemixni],
  data() {
    return {
      queryparam: {
        pagesize: 5,
        pagenum: 1,
        tradecode: "",
        tradename: ""
      },
      //添加

      addroles: {
        tradecode: [{ required: true, message: "输入名称", trigger: "blur" }],
        tradename: [{ required: true, message: "输入描述", trigger: "blur" }]
      },
      addparam: {
        tradename: "",
        tradecode: "",
        tradedesc: "",
        flag: "0"
      },
      modparam: {
        id: "",
        tradename: "",
        tradecode: "",
        tradedesc: "",
        flag: "0"
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //属性数据
    fetchData() {
      this.listLoading = true;
      list(this.queryparam).then(response => {
        this.list = response.data.list;
        this.total = response.data.totalsize;
        this.listLoading = false;
      });
    },

    addsub() {
      this.$refs.addfrom.validate(valid => {
        if (valid) {
          this.isDisable = true;
          add(this.addparam).then(response => {
            this.isDisable = false;
            this.fetchData();
            this.closeAdd();
          });
        } else {
          return false;
        }
      });
    },
    openMod(row) {
      this.modparam = Object.assign({}, row);
      this.modVisible = true;
    },
    subMod() {
      this.$refs.modfrom.validate(valid => {
        if (valid) {
          this.isDisable = true;
          mod(this.modparam).then(response => {
            this.isDisable = false;
            this.fetchData();
            this.closeMod();
          });
        } else {
          return false;
        }
      });
    },
    //删除
    handleDelete(row) {
      this.$confirm("确定删除" + row.tradename, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del({ id: row.id }).then(response => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style>
.el-pagination {
  margin-top: 10px;
  float: right;
}
.query {
  margin-top: 10px;
  float: right;
}
</style>